package com.oplink.webbase.modules.sys.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.oplink.webbase.common.utils.CacheUtils;
import com.oplink.webbase.common.utils.SpringContextHolder;
import com.oplink.webbase.common.utils.UserUtils;
import com.oplink.webbase.modules.sys.dao.ConfigDao;
import com.oplink.webbase.modules.sys.entity.Config;

/**
 * 
 * <p>
 * Title: 模块工具类
 * </p>
 * <p>
 * Description: 构造指定模块页面表单
 * </p>
 * <p>
 * Copyright: Copyright (c) 2014
 * </p>
 * <p>
 * Company: Oplink Communications
 * </p>
 * 
 * @author
 ** @serialData 2014-9-5 上午9:08:13
 * @version 1.0
 */

public class ModuleUtils {
	public static final String CACHE_MODULE_MAP = "moduleMap";
	private static ConfigDao configDao = SpringContextHolder.getBean(ConfigDao.class);

	/**
	 * 获取子表单对象
	 * 
	 * @param type
	 * @return
	 */
	public static List<Config> getChildFormConfig(String type) {
		List<Config> outConfig = new ArrayList<Config>();
		List<Config> listConfig = getModuleList(type);
		for (Config config : listConfig) {
			String childFormType = config.getChildFormList();
			String[] list = childFormType.split("\\,");
			// 查出对应的config对象
			for (String childType : list) {
				outConfig.add(getModuleList(childType).get(0));
			}
		}
		return outConfig;
	}

	/**
	 * 返回单条记录
	 * 
	 * @param catalog
	 * @return
	 */
	public static Config getOneConfig(String catalog) {
		return getModuleList(catalog).get(0);
	}

	/**
	 * 通过id来获取config对象的label
	 * 
	 * @param catalog 目录
	 * @param id id
	 * @return 对应的label
	 */
	public static String getConfigLable(String catalog, String idStr) {
		List<Config> listTemp = getModuleList(catalog);
		Long id = Long.parseLong(idStr);
		for (Config config : listTemp) {
			if (config.getId().equals(id)) {
				return config.getLabel();
			}
		}
		return null;
	}

	/**
	 * 将字符串转换成List
	 * 
	 * @param listStr
	 * @return
	 */
	public static List<String> getChildFormList(String listStr) {
		List<String> out = new ArrayList<String>();
		if (listStr != null) {
			String[] list = listStr.split("\\,");
			for (String item : list) {
				out.add(item);
			}
		}
		return out;
	}

	public static List<Config> getModuleList(String catalog) {
		@SuppressWarnings("unchecked")
		Map<String, List<Config>> cfgMap = (Map<String, List<Config>>) CacheUtils.get(CACHE_MODULE_MAP);
		List<Config> configList = Lists.newArrayList();
		if (cfgMap == null) {
			cfgMap = Maps.newHashMap();
		}

		List<Config> cacheList = cfgMap.get(catalog);
		if (cacheList != null) {
			configList = Lists.newArrayList(cacheList);
		} else {
			List<Config> listConfig = configDao.findAllListByIdSort(UserUtils.getCurrentUserId(), catalog);
			cfgMap.put(catalog, listConfig);
			CacheUtils.put(CACHE_MODULE_MAP, cfgMap);
			configList = Lists.newArrayList(listConfig);
		}
		return configList;
	}
}
